#导包
from api.login import LoginAPI
from api.course import CourseAPI
#创建测试类
class TestAddCourseAPI:
    TOKEN = None
    def setup_method(self):
        #初始化接口类
        self.login_api = LoginAPI()
        self.course_api = CourseAPI()
        #登录成功
        #获取验证码
        res_v = self.login_api.get_verify_code()
        #登录
        login_data ={
            "username": "admin",
            "password": "HM_2023_test",
            "code": 2,
            "uuid": res_v.json().get("uuid")
        }
        res_l =self.login_api.login(test_data=login_data)
        #提取登录成功的token数据并保存
        TestAddCourseAPI.TOKEN = res_l.json().get("token")
        print(TestAddCourseAPI.TOKEN)
    def teardown(self):
        pass
    def test01_success(self):
        add_data = {
            "name":"123456",
            "subject":"6",
            "price":899,
            "applicablePerson":"2"
        }
        response = self.course_api.add_course(test_data=add_data,token=TestAddCourseAPI.TOKEN)
        print(response.json())
        #断言响应码
        assert 200 ==response.status_code
        #断言返回数据中包含的文字
        assert '成功' in response.text
        #断言json返回数据code值
        assert 200 ==response.json().get("code")

    def test02_fail(self):
        add_data = {
            "name": "123456",
            "subject": "6",
            "price": 899,
            "applicablePerson": "2"
        }
        response = self.course_api.add_course(test_data=add_data, token="xxx")
        print(response.json())
        # 断言响应码
        assert 200 == response.status_code
        # 断言返回数据中包含的文字
        assert '认证失败' in response.text
        # 断言json返回数据code值
        assert 401 == response.json().get("code")